<?php

$objXajax->registerFunction("frmroomproblem_loadGrid");
$objXajax->registerFunction("frmroomproblem_edit");
$objXajax->registerFunction("frmroomproblem_save");
$objXajax->registerFunction("frmroomproblem_room");
$objXajax->registerFunction("frmroomproblem_type");
    
function frmroomproblem_loadGrid(){     

    global $lang,$path;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $lang ='vi';
    $arrGrid = frmroomproblem_getGrid();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('../templates/'.$lang.'/'.$path.'/main_roomproblem_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmroomproblem_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('frmroomproblem_tbl');");                              
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function  frmroomproblem_save($params){
    global $objDbSelect;
    //echo '<pre>'; print_r($params);die();
    $objResponse = new xajaxResponse();
    $room = $params['room'] ;
    $type = $params['type'] ;
    $description  = addslashes($params['description']);
    $edit = $params['hdEdit'];
    $id= $params['hdId'];
    $hotel_id =  $_SESSION['current']['hotel_id'];
    $user_id =  $_SESSION['current']['user_id'];
    if($edit==1){
        $sql = "UPDATE tbl_problem SET problem_type_id='$type',room_id='$room',description='$description',date_update=now(),user_update='$user_id'
        WHERE id ='$id'";
    }else{
         $sql = "INSERT INTO tbl_problem(problem_type_id,room_id,description,hotel_id,date_created,user_created,date_update,user_update) 
                                value('$type','$room','$description','$hotel_id',now(),'$user_id',now(),'$user_id')";   
    }

    $objDbSelect->Execute($sql);
    $objResponse->addScript('changeWaiting(1);');
    $objResponse->addScript('xajax_frmroomproblem_loadGrid();');
    return $objResponse->getXML(); 
} 
  
function  frmroomproblem_getGrid(){
    global $objDbSelect,$lang;
    $hotel_id = $_SESSION['current']['hotel_id'];
    $sqlSelect = " SELECT p.*,pt.name_$lang as type_problem,r.name as room FROM tbl_problem as p join tbl_problem_type as pt on p.problem_type_id = pt.id 
    left join tbl_room as r on p.room_id = r.id WHERE hotel_id = '$hotel_id'  order by p.date_created desc";
    return $objDbSelect->GetArray($sqlSelect);
}  

function  frmroomproblem_edit($id){
    global $objDbSelect;
    $sql = "SELECT *,REPLACE(REPLACE(description, '\n\r', '<br/>'),'\n','<br>') as description1 FROM tbl_problem WHERE id = '$id'";
    $arr = $objDbSelect->GetArray($sql);
    $objResponse = new xajaxResponse();
    $room_id = $arr[0]['room_id'];
    $problem_type_id = $arr[0]['problem_type_id'];
    $description = addslashes($arr[0]['description1']);
    $objResponse->addScript("$('#frmroomprobleM_room').val('$room_id');");
    $objResponse->addScript("$('#frmroomprolem_type').val('$problem_type_id');");
    $objResponse->addScript("$('#frmroomproblem_description').val('$description');");
    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML();
} 

function  frmroomproblem_delete($id){
    global $lang;
    $objResponse = new xajaxResponse();
    return $objResponse->getXML();
}

function  frmroomproblem_room(){
    global $lang;
    $objResponse = new xajaxResponse();
    $cbbHotel = createCbbRoomByHotel('frmroomproblem_room');
    $objResponse->addAssign('frmroomproblem_tdroom','innerHTML',"$cbbHotel");
    return $objResponse->getXML();
}
function  frmroomproblem_type(){
    global $lang,$objDbSelect;
    $objResponse = new xajaxResponse();
    $sql = " SELECT id,name_$lang as name FROM tbl_problem_type Where active = 1";

    $arr = $objDbSelect->GetArray($sql);
    //$cls = $clsrequired? "class='cbbRequired'":'';
    $result ="<select id='frmroomprolem_type' class='cbbRequired'><option value=''>--Chọn--</option>";
    for($i=0;$i<count($arr);$i++){
        $name = $arr[$i]['name'];
        $id   = $arr[$i]['id'];
        $result .= "<option value='$id'>$name</option>";
    }
    $result.="</select>";
    $objResponse->addAssign('frmroomproblem_tdtype','innerHTML',"$result");
    return $objResponse->getXML();
}


# end region process data
?>
